#include "AntiGravityShutter.h"
#include "RSManager.h"
#include "Sprite.h"
#include "Box.h"
#include "ShareComponents.h"
#include "ScubaSoldierBullet.h"

AntiGravityShutter::AntiGravityShutter(void) : Object(new Box(), ObjEnemy_DYNAMID)
{
	init();
}


AntiGravityShutter::~AntiGravityShutter(void)
{
}

void AntiGravityShutter::init()
{
	_appear = RSManager::getInstance()->getSprite(SHUTTER_APPEAR);
	_disappear = RSManager::getInstance()->getSprite(SHUTTER_DISAPPEAR);
	_waiting = RSManager::getInstance()->getSprite(SHUTTER_WAITING);
	_sprite = _appear;	
	_posY = new float[3];
	_posY[0] = 320;
	_posY[1] = 180;
	_posY[2] = 260;
	delay = 0;
	_delay = 0;
	_index = -1;
	_hits = 32;
	_isAlive = true;
	_hire = true;
	_box->_size = _sprite->getRenderSize();
	for(int i = 0; i < 5; i++)
	{
		ScubaSoldierBullet* gr = new ScubaSoldierBullet(EFFECT_EXPLODE1);
		_listInit.push_back(gr);
	}
	_posBullet = D3DXVECTOR2(50, 60);
}

void AntiGravityShutter::update()
{
	delay += GAME_TIME;
	if (delay < 3000)
	{
		_hire = true;
		setPosition();
	}
	else
	{
		_hire = false;		
		if(_appear->GetCurrentIndex() == 9)
		{
			_sprite = _waiting;
			_appear->Reset(0);
		}
		if(_disappear->GetCurrentIndex() == 9)
		{
			_sprite = _appear;
			_disappear->Reset(0);
			delay = 0;
		}
		if(delay > 9000)		
			_sprite = _disappear;	

		_delay+=GAME_TIME;
		if(_delay >= 200)
		{
			_sprite->Next();
			shoot();
			_delay = 0;
		}
	}	
	std::list<ScubaSoldierBullet*>::iterator gr = _listUpdate.begin();
	while (gr != _listUpdate.end())
	{
		if((*gr)->isAlive())
		{
			//gl_MainGame->addWorldObject(*gr);
			(*gr++)->update();			
		}
		else
		{			
			_listInit.push_back(*gr);
			_listUpdate.remove(*gr++);	
		}
	}			
}

void AntiGravityShutter::render()
{
	if(!_hire)
		_sprite->Render(_box->_position);
	for(ScubaSoldierBullet* gr : _listUpdate)
		if(gr->isAlive())
			gr->render();
}

Box* AntiGravityShutter::getCollisionBox()
{
	return _box;
}

void AntiGravityShutter::setPosition()
{
	if(_index == 2)
		_index = -1;
	_box->_position.y = _posY[++_index];
	//_box->_position.y = 300;
	_box->_position.x = gl_player->getCenterPos().x - _posBullet.x;
}

void AntiGravityShutter::shoot()
{
	if(_listInit.size() != 0)
	{
		ScubaSoldierBullet* grenade = _listInit.front();		
		grenade->process(ObjEnemy_DYNAMID, _box->_position + _posBullet, -4, 90);
		_listUpdate.push_back(grenade);
		_listInit.remove(grenade);
	}	
}
